package top.humbleyuan.backtrack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author HumbleYuan
 * @Date 2020/5/18 18:30
 * @Des 电话号码组合
 */
public class LeetCode_17 {
    static Map<Character, String> phone = new HashMap<Character, String>() {{
        put('2', "abc");
        put('3', "def");
        put('4', "ghi");
        put('5', "jkl");
        put('6', "mno");
        put('7', "pqrs");
        put('8', "tuv");
        put('9', "wxyz");
    }};

    static List<String> res = new ArrayList<>();

    public static void main(String[] args) {
        String s = "345";
        dfs(s, "");
    }

    public static void dfs(String s, String curStr) {
        if(s.length() == 0) {
            res.add(curStr);
            return ;
        }

        /**
         * 以每个数字的所有字母为循环，依次回溯
         */
        // 获取当前数字
        Character c = s.charAt(0);
        String letters = phone.get(c);

        for (int i = 0; i < letters.length(); i++) {
            curStr += letters.charAt(i);

            // 深度遍历
            // 当长度是1时，substring(1)不会报错，substring(2)会报错，len < 0
            dfs(s.substring(1), curStr);

            // 回溯
            curStr = curStr.substring(0, curStr.length()-1);
        }
    }
}
